//提示信息对象
let notice;
//当前操作的列对象
let cols;
let isSuccess; //表格对象
// import converterDate from '../learn/converterDate'
layui.config({
    base: '../lay/dist/'
});
layui.use(['form', 'table', 'laydate', 'notice','upload'], function () {
    var $ = layui.jquery,
        form = layui.form,
        laydate = layui.laydate,
        upload = layui.upload,
        table = layui.table;
    notice = layui.notice;

    laydate.render({
        elem: '#begin'
        ,theme: '#393D49'

    });
    laydate.render({
        elem: '#end'
        ,theme: '#393D49'

    });

    upload.render({
        elem: '#test3'
        ,url: '/even-subj/upload' //改成您自己的上传接口
        ,accept: 'file' //普通文件
        ,exts: 'xls|xlsx' //只允许上传表格文件
        ,done: function(res){
            if (res.success) {
                table.reload('checkClass', {
                    url: '/even-subj/all'
                })
            } else {
                notice.error("上传失败" + res.data);
            }
        },choose: function(obj){
            //将每次选择的文件追加到文件队列
            obj.preview(function(index, file, result) {
                console.log(index); //得到文件索引
                console.log(file); //得到文件对象
                console.log(result); //得到文件base64编码，比如图片

                //obj.resetFile(index, file, '123.jpg'); //重命名文件名，layui 2.3.0 开始新增

                //这里还可以做一些 append 文件列表 DOM 的操作

                //obj.upload(index, file); //对上传失败的单个文件重新上传，一般在某个事件中使用
                //delete files[index]; //删除列表中对应的文件，一般在某个事件中使用
            })
        }
    });
    /**
     * 表格主题渲染
     */
    table.render({
        elem: '#checkClass',
        url: '/even-subj/all',
        method:"post",
        parseData: function (res) {
            console.log(res.success)
            return {
                "code": 0, //res.success? 0 : 1
                "count": res.total,
                "data": res.data

            }
        },
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [
            [
                {
                    field: 'xid', width: 80, title: '序号', sort: true, templet: function (d) {
                        return d.LAY_INDEX;
                    }
                },
                {field: 'id', width: 100, title: 'ID', sort: true, hide: true},
                {
                    field: 'date', width: 150, title: '日期', sort: true,templet: function (d) {
                        return ConverterDate(d.date);
                    }
                },
                {field: 'className',  title: '班级'},
                {field: 'classroomName', title: '教室名称'},
                {field: 'classCount',  title: '应到人数', sort: true},
                {field: 'teacherName',  title: '值班老师', },
                {field: 'mess', title: '授课内容',},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]
        ],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line'
    });


    /**
     * 监听搜索操作
     */
    form.on('submit(data-search-btn)', function (data) {
        var result = data.field;
        console.log(result)
        // return false
        //执行搜索重载
        table.reload('checkClass', {
            url: '/even-subj/search'
            , where: {
                start: result.beginTime,
                end: result.endTime,
                className: result.className,
            }
        },      );
        notice.success("搜索执行成功！");

        return false;
    });
    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'selectAll') { //
            table.reload('checkClass', {
                url: '/even-subj/all'
            })
            notice.success("查询成功！");
        } else if (obj.event === 'selectIsRed') {


        }else if (obj.event === 'selectNow') {  //
            let date= ConverterDate();
            table.reload('checkClass', {
                url: '/even-subj/search',
                where: {
                    start: date
                }
            })
            notice.success("查询成功！");
        }else if (obj.event === 'addInfo') {  //
            let index = layer.open({
                title: '添加晚自习记录',
                type: 2,
                offset:100,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['70%', '70%'],
                content: '/even-subj/add',
                end: ()=> {
                    if (isSuccess) {
                        table.reload('checkClass', {
                            url: '/even-subj/all'
                        })
                    }
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;

        }
    });
    /**
     * 工具行监听(删除/编辑)
     * */
    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        cols = obj;
        if (obj.event === 'edit') {
            console.log(data.id);
            var index = layer.open({
                title: '编辑用户',
                type: 2,
                shade: 0.2,
                offset:100,
                maxmin: true,
                shadeClose: true,
                area: ['70%', '50%'],
                content: '/even-subj/edit?id=' + data.id,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('是否要删除此条记录？', function (index) {
                $.post("/even-subj/del", {id: obj.data.id}, function (result) {
                    // console.log(result)
                    if (result.success) {
                        notice.success("删除成功！");
                        obj.del()
                    } else {
                        notice.warning("删除失败！");
                    }
                },"json")
                layer.close(index);
            }, function (index) {
                layer.close(index);
            },'json');

        }

    });


    /**
     * 设置提示信息属性
     * @type {{hideEasing: string, positionClass: string, hideDuration: string, debug: boolean, closeButton: boolean, extendedTimeOut: string, showEasing: string, onclick: null, showDuration: string, iconClass: string, timeOut: string}}
     */
    notice.options = {
        closeButton: true,//显示关闭按钮
        debug: false,//启用debug
        positionClass: "toast-bottom-right",//弹出的位置,
        showDuration: "300",//显示的时间
        hideDuration: "1000",//消失的时间
        timeOut: "2000",//停留的时间
        extendedTimeOut: "1000",//控制时间
        showEasing: "swing",//显示时的动画缓冲方式
        hideEasing: "linear",//消失时的动画缓冲方式
        iconClass: 'toast-info', // 自定义图标，有内置，如不需要则传空 支持layui内置图标/自定义iconfont类名
        onclick: null, // 点击关闭回调
    };

    // notice.warning("");信息状态
    // notice.info("");
    // notice.error("");
    // notice.success("");
});

/**
 * 子窗体编辑后父窗体动态改变
 * @param result 修改||添加的结果
 * @param childData
 */
function parentPage( childData) {
    console.log(childData);
    layer.closeAll()
    if (childData) {
        cols.update({
            //更新列；
            id: childData.id,
            date: childData.date,
            className: childData.className,
            classCount: childData.classCount,
            classroomName: childData.classroomName,
            teacherName: childData.teacherName,
            mess: childData.mess
        });

    } else {
        isSuccess=true;
    }

}
